package org.xhy.sniffer.trace;


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class TraceContext {


    private TraceSegment segment;




    public TraceContext() {
        segment = new TraceSegment();
    }


    public AbstractSpan getPeekSpan(){
        return segment.activeSpanStack();
    }

    public void createEntrySpan(String operationName,CommentDefine comment){
        createEntrySpan(operationName,comment,"");
    }

    public void createEntrySpan(String operationName,CommentDefine comment,String peer){
        segment.createEntrySpan(operationName, comment,peer);
    }

    public void createExitSpan(){
        segment.createExitSpan();
    }

    public static class ListenerManager{

        private static List<TracingContextListener> listeners = new ArrayList<>();

        public static void add(TracingContextListener listener){
            listeners.add(listener);
        }

        public static void notifyFinish(TraceSegment segment){
            for (TracingContextListener listener : listeners) {
                listener.afterFinished(segment);
            }
        }

        public static void remove(){
            listeners.remove(listeners);
        }
    }

}
